﻿using AbpEntityFrameworkcore.DbContext;
using AbpToDomain.EntityInfo;
using AbpToDomain.View;
using Microsoft.EntityFrameworkCore;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Domain.Repositories;

namespace AbpApplication
{
   public  class QueryDynamic
    {
        private readonly MyDbContext context;
     

        public QueryDynamic(MyDbContext context)
        {
            this.context = context;
        }

        public List<WarehouseLine> QueryDynameic(int?Xaxis,int?Yaxis,int?Zaxis,Guid? Id_Warehouse)
        {

            IQueryable<WarehouseLine> warehouseLines = context.warehouseLines;

            if (!string.IsNullOrEmpty(Xaxis.ToString()))
            {
                warehouseLines=warehouseLines.Where(x=> x.Xaxis == Xaxis);
            }

            if (!string.IsNullOrEmpty(Yaxis.ToString()))
            {
                warehouseLines = warehouseLines.Where(x => x.Yaxis == Yaxis);
            }
            if (!string.IsNullOrEmpty(Zaxis.ToString()))
            {
                warehouseLines = warehouseLines.Where(x => x.Zaxis == Zaxis);
            }
            if (!string.IsNullOrEmpty(Id_Warehouse.ToString()))
            {
                warehouseLines = warehouseLines.Where(x => x.TheWarehouse.Id_Warehouse == Id_Warehouse);
            }
            return warehouseLines.ToList();
        }
        public List<View_Material> QueryDynameicView_Material(string? BoxPositionCode)
        {
            IQueryable<View_Material> view_Materials=  context.View_Materials;


            if (!string.IsNullOrEmpty(BoxPositionCode))
            {
                view_Materials = view_Materials.Where(x => x.BoxPositionCode.Contains( BoxPositionCode));
            }

            return view_Materials.ToList();
        }

        public List<View_TaskOrder> QueryDynameicView_TaskOder(string?Type,string?OrderNo,
            string?BoxCode,string?State,string?WarehouseCode,string?InlethouseCode,string?TaskOrderCode,
            string? groupPriority,string? taskPriority,int? Machine)
        {
            IQueryable<View_TaskOrder> view_TaskOrders=context.View_TaskOrders;
            if(!string.IsNullOrEmpty(Type))
            {
                view_TaskOrders=view_TaskOrders.Where(x => x.Type == Type);
            }
            if(!string.IsNullOrEmpty(OrderNo))
            {
                view_TaskOrders=view_TaskOrders.Where(x=>x.OrderNo .Contains( OrderNo));
            }
            if(!string.IsNullOrEmpty(BoxCode))
            {
                view_TaskOrders=view_TaskOrders.Where(x=>x.BoxCode == BoxCode);
            }
            if(!string.IsNullOrEmpty(State))
            {
                view_TaskOrders=view_TaskOrders.Where(x=>x.State == State);
            }
            if(!string.IsNullOrEmpty(WarehouseCode))
            {
                view_TaskOrders = view_TaskOrders.Where(x => x.WarehouseCode == WarehouseCode);
            }
            if(!string.IsNullOrEmpty(InlethouseCode))
            {
                view_TaskOrders = view_TaskOrders.Where(x => x.InlethouseCode .Contains( InlethouseCode));
            }
            if(!string.IsNullOrEmpty(TaskOrderCode))
            {
                view_TaskOrders = view_TaskOrders.Where(x => x.TaskOrderCode == TaskOrderCode);
            }
            if (!string.IsNullOrEmpty(Machine.ToString()))
            {
                view_TaskOrders = view_TaskOrders.Where(x => x.Machine == Machine);
            }
            return view_TaskOrders.ToList();
        }

        public List<View_Box_WarehouseLine> QueryDynameicView_Box_WarehouseLine(string?BoxCode, string? WarehouseLineCode)
        {
            IQueryable<View_Box_WarehouseLine> view_Box_WarehouseLines = context.View_Box_WarehouseLines;
            if (!string.IsNullOrEmpty(BoxCode))
            {
                view_Box_WarehouseLines = view_Box_WarehouseLines.Where(x => x.BoxCode.Contains(BoxCode));
            }
            if (!string.IsNullOrEmpty(WarehouseLineCode))
            {
                view_Box_WarehouseLines = view_Box_WarehouseLines.Where(x => x.WarehouseLineCode.Contains(WarehouseLineCode));
            }
            return view_Box_WarehouseLines.ToList();


        }

        public List<View_Material_WarehouseLine> QueryDynameicView_Material_WarehouseLine(string? BoxPositionCode,string? MaterialCode,string? WarehouseLineCode,
            string? State, string? Type,DateTime? PutInDate)
        {
            IQueryable<View_Material_WarehouseLine> view_Material_WarehouLines = context.View_Material_WarehouseLines;
         
            if (!string.IsNullOrEmpty(BoxPositionCode))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.BoxPositionCode.Contains(BoxPositionCode));
            }
            if (!string.IsNullOrEmpty(PutInDate.ToString()))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x=>x.PutInDate.Value.Date==PutInDate.Value.Date);
            }
            if (!string.IsNullOrEmpty(MaterialCode))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.MaterialCode.Contains(MaterialCode));
            }
            if (!string.IsNullOrEmpty(WarehouseLineCode))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.WarehouseLineCode.Contains(WarehouseLineCode));
            }
            if (!string.IsNullOrEmpty(State))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.State.Contains(State));
            }
            if (!string.IsNullOrEmpty(Type))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.Type.Contains(Type));
            }
            return view_Material_WarehouLines.ToList();
        }

        public List<View_NeetOutMaterial> QueryDynameicView_View_NeetOutMaterial(string? BoxCode,int?Machine)
        {
           IQueryable<View_NeetOutMaterial> view_Material_WarehouLines = context.View_NeetOutMaterials;

            if (!string.IsNullOrEmpty(BoxCode))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.BoxPositionCode.Contains(BoxCode));
            }
            if (!string.IsNullOrEmpty(Machine.ToString()))
            {
                view_Material_WarehouLines = view_Material_WarehouLines.Where(x => x.Machine==Machine);
            }
            return view_Material_WarehouLines.ToList();
        }
        public List<lognote> QueryDynameicView_View_Lognote(string?Type,DateTime? StartTime,DateTime? EndTime,string? Note,
            string? User, string? Definition, string? MaterialCode, 
            string? MaterialName, string? Batch, string? Size, string? OrderNo, string? BoxCode,
            string? WarehouseLineCode)
        {
            IQueryable<lognote> view_lognote= context.lognotes.OrderByDescending(x => x.DateTime); 
            if (!string.IsNullOrEmpty(Type))
            {
                view_lognote = view_lognote.Where(x => x.Type.Contains(Type));
            }
            if (!string.IsNullOrEmpty(Note))
            {
                view_lognote = view_lognote.Where(x => x.Note.Contains(Note));
            }
            if (!string.IsNullOrEmpty(User))
            {
                view_lognote = view_lognote.Where(x => x.User.Contains(User));
            }
            if (!string.IsNullOrEmpty(Definition))
            {
                view_lognote = view_lognote.Where(x => x.Definition.Contains(Definition));
            }
            if (!string.IsNullOrEmpty(MaterialCode))
            {
                view_lognote = view_lognote.Where(x => x.MaterialCode.Contains(MaterialCode));
            }
            if (!string.IsNullOrEmpty(MaterialName))
            {
                view_lognote = view_lognote.Where(x => x.MaterialName.Contains(MaterialName));
            }
            if (!string.IsNullOrEmpty(Batch))
            {
                view_lognote = view_lognote.Where(x => x.Batch.Contains(Batch));
            }
            if (!string.IsNullOrEmpty(Size))
            {
                view_lognote = view_lognote.Where(x => x.Size.Contains(Size));
            }
            if (!string.IsNullOrEmpty(OrderNo))
            {
                view_lognote = view_lognote.Where(x => x.OrderNo.Contains(OrderNo));
            }
            if (!string.IsNullOrEmpty(BoxCode))
            {
                view_lognote = view_lognote.Where(x => x.BoxCode.Contains(BoxCode));
            }
            if (!string.IsNullOrEmpty(WarehouseLineCode))
            {
                view_lognote = view_lognote.Where(x => x.WarehouseLineCode.Contains(WarehouseLineCode));
            }
            if(!string.IsNullOrEmpty(StartTime.ToString())&& !string.IsNullOrEmpty(EndTime.ToString()))
            {
                view_lognote = view_lognote.Where(x => x.DateTime>=StartTime&&x.DateTime<=EndTime.Value.AddDays(1));
            }

            return view_lognote.ToList();
        }

        public List<MaterialProperty> QueryDynameic_MaterialProperty(string?MaterialPropertyName,string?Type,string? Description)
        {
            IQueryable<MaterialProperty> materialProperties=context.materialProperties;
            if(!string.IsNullOrEmpty(MaterialPropertyName))
            {
                materialProperties =materialProperties.Where(x=>x.MaterialProertyname.Contains(MaterialPropertyName));
            }
            if(!string.IsNullOrEmpty(Description))
            {
                materialProperties = materialProperties.Where(x => x.Description.Contains(Description));
            }
            if (!string.IsNullOrEmpty(Type))
            {
                materialProperties = materialProperties.Where(x => x.Type.Contains(Type));
            }
            return materialProperties.ToList();
        }

    }
}
